// import {}

import { ref, reactive, type Ref } from 'vue'
import { addCate as addCateApi, updateCate as updateCateApi } from '@/api/cate'
import { message } from 'ant-design-vue'
export const visible = ref<boolean>(false)

export const showModal = () => {
  visible.value = true
}

// export function addCate() {
//   showModal()
// }

interface FormState {
  name: string
  id: string
}

export const formState = reactive<FormState>({
  name: '',
  id: ''
})

export default function name() {
  const handleOk = (
    isUpdate: Ref<boolean>,
    formState: FormState,
    cb: Function
  ) => {
    if (isUpdate.value) {
      updateCateApi({
        cate_id: formState.id,
        cate_name: formState.name
      }).then((res: any) => {
        console.log(res)
        if (res.code === 0) {
          message.success(res.msg)

          visible.value = false
          formState.name = ''
          formState.id = ''
          cb()
        } else {
          message.warning(res.msg)
        }
      })
    } else {
      //   增加分类
      addCateApi({
        name: formState.name
      }).then((res: any) => {
        console.log(res)
        if (res.code === 0) {
          message.success(res.msg)

          visible.value = false
          formState.name = ''
          formState.id = ''
          cb() //  刷新一下
        } else {
          message.warning(res.msg)
        }
      })
    }
  }

  return {
    handleOk
  }
}
